package com.cskaoyan.service;

import com.cskaoyan.bean.Region;
import com.cskaoyan.bean.RegionExample;
import com.cskaoyan.mapper.RegionMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegionServiceImpl implements RegionService{

    @Autowired
    RegionMapper regionMapper;
    @Override
    public List<Region> queryRegion() {
        //一级查询
        RegionExample oneRegionExample = new RegionExample();

        oneRegionExample.createCriteria().andTypeEqualTo((byte) 1);

        List<Region> regions1 = regionMapper.selectByExample(oneRegionExample);

        //二级查询

        RegionExample twoRegionExample = new RegionExample();

        twoRegionExample.createCriteria().andTypeEqualTo((byte) 2);

        List<Region> regions2 = regionMapper.selectByExample(twoRegionExample);

        //三级查询

        RegionExample threeRegionExample = new RegionExample();

        twoRegionExample.createCriteria().andTypeEqualTo((byte) 3);

        List<Region> regions3 = regionMapper.selectByExample(threeRegionExample);

        for (int i = 0; i < regions2 .size(); i++) {
            for (int j = 0; j < regions3 .size(); j++) {
                Region two =  regions2.get(i);
                Region three = regions3.get(j);
                if (three.getPid().equals(two.getId())) {
                    two.getChildren().add(three);
                }
            }
        }

        for (int i = 0; i < regions1.size(); i++) {
            for (int j = 0; j < regions2.size(); j++) {
                Region one = regions1.get(i);
                Region two = regions2.get(j);
                if (two.getPid().equals(one.getId())) {
                    one.getChildren().add(two);
                }
            }
        }
        return regions1;
    }
}
